package com.ncst.backend.controller;

import com.ncst.backend.common.Result;
import com.ncst.backend.domain.Employee;
import com.ncst.backend.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工控制器
 */
@Api(tags = "员工管理")
@Slf4j
@RestController
@RequestMapping("/employees")
public class EmployeeController {
    
    @Autowired
    private EmployeeService employeeService;
    
    @ApiOperation("根据ID查询员工")
    @GetMapping("/{id}")
    public Result<Employee> getById(@ApiParam("员工ID") @PathVariable Integer id) {
        Employee employee = employeeService.getById(id);
        return employee != null ? Result.success(employee) : Result.notFound("员工不存在");
    }
    
    @ApiOperation("查询所有员工")
    @GetMapping
    public Result<List<Employee>> getAll() {
        return Result.success(employeeService.getAll());
    }
    
    @ApiOperation("根据条件查询员工列表")
    @PostMapping("/search")
    public Result<List<Employee>> getByCondition(@ApiParam("查询条件") @RequestBody Employee employee) {
        return Result.success(employeeService.getByCondition(employee));
    }
    
    @ApiOperation("分页查询员工")
    @GetMapping("/page")
    public Result<List<Employee>> getByPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(employeeService.getByPage(pageNum, pageSize));
    }
    
    @ApiOperation("统计员工总数")
    @GetMapping("/count")
    public Result<Long> countAll() {
        return Result.success(employeeService.countAll());
    }
    
    @ApiOperation("根据条件统计员工数量")
    @PostMapping("/count")
    public Result<Long> countByCondition(@ApiParam("查询条件") @RequestBody Employee employee) {
        return Result.success(employeeService.countByCondition(employee));
    }
    
    @ApiOperation("新增员工")
    @PostMapping
    public Result<Void> save(@ApiParam("员工信息") @RequestBody Employee employee) {
        return employeeService.save(employee) ? Result.success("新增成功") : Result.error("新增失败");
    }
    
    @ApiOperation("批量新增员工")
    @PostMapping("/batch")
    public Result<Void> saveBatch(@ApiParam("员工列表") @RequestBody List<Employee> list) {
        return employeeService.saveBatch(list) ? Result.success("批量新增成功") : Result.error("批量新增失败");
    }
    
    @ApiOperation("根据ID更新员工")
    @PutMapping("/{id}")
    public Result<Void> updateById(@ApiParam("员工ID") @PathVariable Integer id, @ApiParam("员工信息") @RequestBody Employee employee) {
        employee.setId(id);
        return employeeService.updateById(employee) ? Result.success("更新成功") : Result.error("更新失败");
    }
    
    @ApiOperation("根据ID删除员工")
    @DeleteMapping("/{id}")
    public Result<Void> removeById(@ApiParam("员工ID") @PathVariable Integer id) {
        return employeeService.removeById(id) ? Result.success("删除成功") : Result.error("删除失败");
    }
    
    @ApiOperation("批量删除员工")
    @DeleteMapping("/batch")
    public Result<Void> removeByIds(@ApiParam("员工ID列表") @RequestBody List<Integer> ids) {
        return employeeService.removeByIds(ids) ? Result.success("批量删除成功") : Result.error("批量删除失败");
    }
}
